package Server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;

/**
 * <p>Title: Authentication.java</p>
 *
 * <p>Description: Used when authentication of the user is needed before access
 * is given to certain files. This class will take the information submitted by
 * the user and check the .htaccess file to see if that user has access to the
 * file he/she is trying to view. Two main functions exist in this class. One
 * function to check if authentication is needed, and another to decode and
 * validate the authentication data once it has been received by the server.</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 *
 * @Author: YingLuan , Yves Galang
 * @Course: CSC667
 * @Date: 2012 Spring
 *
 * @original author: Luping May and Tracie Hong
 * @version 1.0
 */
public class Authentication {

    public Authentication() {
        _authentication = new HashMap();
    }

    public void addAuth(String d, String n, String t, String f, String u) throws FileNotFoundException, IOException {
        _authentication.put(d, new Block(n, t, f, u));
    }

    /**
     * Checks the incoming information from the client against the user file to
     * see if the authentication information is correct. If it is correct, the
     * user can proceed, otherwise he or she is blocked and not allowed to access
     * files. This class uses the Base64Decoder class to check information.
     *
     * @param input String passed in through the header which is encoded. Use the
     *   Base64Decoder to decode this information so it can be used to check
     *   against in the user file.
     * @return true if data passed in matches what is in the user file, false
     *   otherwise
     */
    public boolean pathSecured(String path) {
        boolean isSecured = false;
        Set<String> _path_list = _authentication.keySet();
        for (String _auth_path : _path_list) {
            if (path.contains(_auth_path)) {
                isSecured = true;
                _secured_path = _auth_path;
                break;
            }
        }
        return isSecured;
    }

    public boolean checkAuth(String user) {
        boolean isAuthorized = false;
        if (_authentication.get(_secured_path).checkUser(user)) {
            isAuthorized = true;
        }
        return isAuthorized;
    }
    private HashMap<String, Block> _authentication;
    private String _secured_path;
}

class Block {

    Block(String n, String t, String f, String u) throws FileNotFoundException, IOException {
        _name = n;
        _type = t;
        _file = f;
        _user = new ArrayList();
        BufferedReader br = new BufferedReader(new FileReader(new File(_file)));
        Base64Encoder encoder;
        String tmp;
        if (u.equals("valid-user")) {
            while ((tmp = br.readLine()) != null) {
                encoder = new Base64Encoder(tmp);
                _user.add(encoder.processString());
            }
        } else {
            String[] split = u.split(",");
            ArrayList list = new ArrayList();
            list.addAll(Arrays.asList(split));
            while ((tmp = br.readLine()) != null) {
                if (list.contains(tmp.substring(0, tmp.indexOf(":")))) {
                    encoder = new Base64Encoder(tmp);
                    _user.add(encoder.processString());
                }
            }
        }
    }

    public boolean checkUser(String user) {
        return _user.contains(user);
    }
    private String _name;
    private String _type;
    private String _file;
    private ArrayList _user;
}
